import os
import shutil
import sys

from utils import json_util


def gen_path(path):
    if not os.path.exists(path):
        os.makedirs(path)  # 创建一个文件夹


def del_blank_doc(path):
    for root, dirs, files in os.walk(path):
        for file in files:
            src_file = os.path.join(root, file)
            if os.path.getsize(src_file) == 0:
                print("删除" + file)
                os.remove(src_file)


def get_file_names(path):
    validation_dict = []
    for root, dirs, files in os.walk(path):
        for file in files:
            src_file = os.path.join(root, file)
            if os.path.getsize(src_file) == 0:
                print("删除" + file)
                os.remove(src_file)
            else:
                validation_dict.append(file.split('.')[0])
    return validation_dict


def get_file_path_names(path):
    validation_dict = []
    for root, dirs, files in os.walk(path):
        for file in files:
            src_file = os.path.join(root, file)
            if os.path.getsize(src_file) == 0:
                print("删除" + file)
                os.remove(src_file)
            else:
                result_path = (root + '\\' + file).replace('/', '\\')
                validation_dict.append(result_path)
    return validation_dict


# result = []
def get_doc_info(doc):
    result = []
    while 1:
        line = doc.readline()
        if line == '':
            doc.close()
            return result
        line = line[:-1]
        if line == '':
            doc.close()
            return result
        result.append(line)


# result = []
def get_path_doc_info(path):
    result = []
    if not os.path.exists(path):
        return result
    doc = open(path, mode='r', encoding='utf-8')
    while 1:
        line = doc.readline()
        if line == '':
            doc.close()
            return result
        if line[-1] == '\n':
            line = line[:-1]
            if line == '':
                doc.close()
                return result
        result.append(json_util.info_to_json(line))


def get_doc_str_list(path):
    result = []
    if not os.path.exists(path):
        return result
    doc = open(path, mode='r', encoding='utf-8')
    while 1:
        line = doc.readline()
        if line == '':
            doc.close()
            return result
        if line[-1] == '\n':
            line = line[:-1]
        result.append(line)


# result = []
def get_doc_info_null(path):
    result = []
    if not os.path.exists(path):
        return result
    doc = open(path, mode='r', encoding='utf-8')
    while 1:
        line = doc.readline()
        if line == '':
            doc.close()
            return result
        line = line[:-1]
        if line == '':
            doc.close()
            return result
        result.append(line)


# data = [] 每个list中参数是一行
# name 文件名称
def gen_new_doc(path, name, data):
    if not os.path.exists(path):
        os.makedirs(path)  # 创建一个文件夹
    new_doc = open(os.path.join(path, name), mode='w', encoding='utf-8')
    for line in data:
        new_doc.write(str(line) + '\n')
    new_doc.close()


# data = {}每个参数value是一行
# name 文件名称
def gen_a_doc(path, name, stock_dict):
    if not os.path.exists(path):
        os.makedirs(path)  # 创建一个文件夹
    new_doc = open(os.path.join(path, name), mode='w', encoding='utf-8')
    for key in stock_dict.keys():
        new_doc.write(str(stock_dict[key]) + '\n')
    new_doc.close()


# data = {} 存放value值
# name 文件名称
def get_a_doc_dict(path, key_name):
    info_list = get_path_doc_info(path)
    info_dict = {}
    for info in info_list:
        info = json_util.info_to_json(info)
        info_dict[info[key_name]] = info
    return info_dict


def del_path_all_files(path):
    for root, dirs, files in os.walk(path):
        for file in files:
            src_file = os.path.join(root, file)
            print("删除" + file)
            os.remove(src_file)


def del_doc(path_file_name):
    os.remove(path_file_name)


# 复制文件
# name为空 复制文件夹
def copy_doc(form, to):
    # adding exception handling
    try:
        shutil.copy(form, to)
    except IOError as e:
        print("Unable to copy file. %s" % e)
    except:
        print("Unexpected error:", sys.exc_info())


if __name__ == '__main__':
    print()
    del_path_all_files(os.path.join(os.getcwd().split('StockMonitor')[0] + 'StockMonitor', 'utils', 'cache'))
